package com.aidex.admin.controller.system;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.query.QueryGenerator;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.framework.cache.ConfigUtils;
import com.aidex.system.domain.SysConfig;
import com.aidex.system.service.SysConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "参数配置")
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController<SysConfigService> {

    @ApiOperation("获取参数配置列表")
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/page")
    public R page(SysConfig entity,
                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                  HttpServletRequest req) {
        QueryWrapper<SysConfig> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        Page<SysConfig> page = new Page<>(pageNo, pageSize);
        IPage<SysConfig> pageList = service.selectPage(page, queryWrapper);
        return R.data(pageList);
    }

    @ApiOperation("根据ID获取参数配置信息")
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{id}")
    public R detail(@PathVariable String id) {
        return R.data(service.selectById(id));
    }

    @ApiOperation("新增参数配置")
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数配置管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R insert(@Validated @RequestBody SysConfig config) {
        if(StringUtils.isEmpty(config.getId())){
            return R.status(service.insert(config));
        }
        return R.status(service.updateById(config));
    }

    @ApiOperation("导出参数配置")
    @Log(title = "参数配置管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping("/export")
    public R export(SysConfig entity, HttpServletRequest req) {
        QueryWrapper<SysConfig> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        List<SysConfig> list = service.selectList(queryWrapper);
        ExcelUtil<SysConfig> util = new ExcelUtil<>(SysConfig.class);
        return util.exportExcel(list, "参数配置");
    }

    @ApiOperation("根据参数键名查询参数值")
    @GetMapping(value = "/configKey/{configKey}")
    public R getConfigKey(@PathVariable String configKey) {
        return R.data(ConfigUtils.getConfigValueByKey(configKey));
    }

    @ApiOperation("删除参数配置")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数配置管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(service.deleteBatchIds(Arrays.asList(ids)));
    }

    @ApiOperation("刷新参数配置缓存")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数配置管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public R refreshCache() {
        service.refreshCache();
        return R.status(true);
    }

    @ApiOperation("校验参数键名是否存在")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.CHECK)
    @GetMapping("/checkConfigKeyUnique")
    public R checkDictDataValueUnique(SysConfig sysConfig) {
        Map<String, String> checkMap = new HashMap<>(1);
        try {
            service.checkConfigKeyUnique(sysConfig);
            checkMap.put("code", "1");
        } catch (Exception e) {
            checkMap.put("code", "2");
        }
        return R.data(checkMap);
    }
}
